package com.yunding.house.utils.wx;
import com.yunding.house.utils.wx.utils.AccessToken;
import net.sf.json.JSONObject;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 * ClassName:NetUtils
 *
 * @Description: 发送接收微信服务器的数据解析及返回
 * @Author:chenzhen Date:2019/3/26 20:36
 * Version 1.0
 * 约饭：c98aedb374dc5c801a0b470d78d63edf_wxaaebfb5803d62bd4
 * wx3f3511f4bd31997f？？？？
 * 112443191240
 * 788b3d8537728167d846da4ebeae7b45
 **/
public class NetUtils {
    //设置token的连接属性
    private static final String GET_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
    //设置APPID
    private static final String APPID="wxc301b0e710ef1aa5";
    //设置APPSECRET
    private static final String APPSECRET="97fb8267b4bdd2ed0806b19feb81c54d";




    //���token
    private static AccessToken at;

    /**
     * ��ָ����λ�÷���post���󣬴���data����
     *
     * @param url  : ���͵�ַ
     * @param data : ����
     * @return
     */
    public static String post(String url, String data) {
        try {
            URL urlObj = new URL(url);
            URLConnection connection = urlObj.openConnection();
            //Ҫ�������ݳ�ȥ������Ҫ����Ϊ�ɷ�������״̬
            connection.setDoOutput(true);
            //��ȡ�����
            OutputStream os = connection.getOutputStream();
            //д������
            os.write(data.getBytes("utf-8"));
            os.close();
            //��ȡ������
            InputStream is = connection.getInputStream();
            byte[] b = new byte[1024];
            int len;
            StringBuilder sb = new StringBuilder();

            while ((len = is.read(b)) != -1) {

                sb.append(new String(b, 0, len));

            }

            return sb.toString();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return data;

    }

    /**
     * ��ָ����λ�÷���get����
     *
     * @param url
     * @return
     */
    public static String get(String url) {
        try {
            URL urlObj = new URL(url);
            //打开连接
            URLConnection connection = urlObj.openConnection();
            InputStream is = connection.getInputStream();
            byte[] b = new byte[1024];
            int len;
            StringBuilder sb = new StringBuilder();

            while ((len = is.read(b)) != -1) {


                sb.append(new String(b, 0, len, "utf-8"));

            }

            return sb.toString();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;

    }

    /**
     * ��ȡtoken
     */
    private static void getToken() {

        String url = GET_TOKEN_URL.replace("APPID", APPID).replace("APPSECRET", APPSECRET);
        String tokenUrl = get(url);
        System.out.println(tokenUrl);
        JSONObject jsonObject = JSONObject.fromObject(tokenUrl);

        String token = jsonObject.getString("access_token");
        String expires = jsonObject.getString("expires_in");

        at = new AccessToken(token, expires);
    }

    /**
     * ���Ⱪ¶��ȡtoken�ķ���
     *
     * @return
     */
    public static String getAccessToken() {
        if (at == null || at.isExpired()) {
            getToken();
        }
        return at.getAccessToken();
    }

    /**
     *识别图中的文字进行处理
     * ����ͼƬʶ��
     *
     * @return : json数组
     */
    /*public static String getPicWords(String urlPath) {
        // ��ʼ��һ��AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

        // ��ѡ�������������Ӳ���
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // ���ýӿ�
        String path = urlPath;
        org.json.JSONObject res = client.basicGeneralUrl(path, new HashMap<String, String>());

        return res.toString(2);
    }*/

}
